import random
import time

class GuessingGame:
    """
    猜数字游戏类
    玩家需要在指定次数内猜测计算机随机生成的数字
    """
    
    def __init__(self):
        """
        初始化游戏设置
        """
        self.min_range = 1
        self.max_range = 100
        self.max_attempts = 7
        self.target_number = 0
        self.attempts = 0
        self.history = []
    
    def generate_number(self):
        """
        生成随机目标数字
        """
        self.target_number = random.randint(self.min_range, self.max_range)
    
    def get_user_guess(self):
        """
        获取并验证用户输入的猜测数字
        """
        while True:
            try:
                guess = int(input("请输入一个{}到{}之间的数字: ".format(self.min_range, self.max_range)))
                if self.min_range <= guess <= self.max_range:
                    return guess
                else:
                    print("请输入{}到{}之间的数字！".format(self.min_range, self.max_range))
            except ValueError:
                print("请输入有效的数字！")
    
    def provide_hint(self, guess):
        """
        根据用户的猜测提供提示
        """
        if guess < self.target_number:
            print("猜小了！")
        else:
            print("猜大了！")
    
    def record_attempt(self, guess):
        """
        记录每次猜测
        """
        self.attempts += 1
        self.history.append((self.attempts, guess))
    
    def display_history(self):
        """
        显示猜测历史
        """
        print("\n猜测历史:")
        for attempt, guess in self.history:
            print("第{}次: {}".format(attempt, guess))
    
    def play_round(self):
        """
        进行一轮游戏
        """
        self.generate_number()
        self.attempts = 0
        self.history = []
        
        print("\n我已经想好了一个{}到{}之间的数字，你有{}次机会来猜测！".format(self.min_range, self.max_range, self.max_attempts))
        
        for _ in range(self.max_attempts):
            guess = self.get_user_guess()
            self.record_attempt(guess)
            
            if guess == self.target_number:
                print("恭喜你！猜对了！目标数字就是{}！".format(self.target_number))
                print("你用了{}次就猜对了！".format(self.attempts))
                self.display_history()
                return True
            else:
                self.provide_hint(guess)
                remaining = self.max_attempts - self.attempts
                if remaining > 0:
                    print("你还剩{}次机会。".format(remaining))
        
        print("\n很遗憾，你用完了所有机会！")
        print("正确的数字是{}。".format(self.target_number))
        self.display_history()
        return False
    
    def start(self):
        """
        启动游戏主循环
        """
        print("欢迎来到猜数字游戏！")
        
        while True:
            self.play_round()
            
            play_again = input("\n是否再玩一次？(y/n): ").lower()
            if play_again != 'y':
                break
        
        print("\n谢谢你玩猜数字游戏！再见！")


if __name__ == "__main__":
    # 程序入口
    try:
        game = GuessingGame()
        game.start()
    except KeyboardInterrupt:
        print("\n\n游戏已中断，再见！")
    except Exception as e:
        print("\n游戏发生错误: {}".format(e))
        print("请稍后再试。")
    finally:
        time.sleep(1)  # 给用户时间看到最终消息